package com.zxy.study.base.app;

import android.app.Application;
import android.content.Intent;
import android.webkit.TracingConfig;

import com.github.moduth.blockcanary.BlockCanary;
import com.tencent.matrix.Matrix;
import com.tencent.matrix.iocanary.IOCanaryPlugin;
import com.tencent.matrix.iocanary.config.IOConfig;
import com.tencent.matrix.resource.ResourcePlugin;
import com.tencent.matrix.resource.config.ResourceConfig;
import com.tencent.matrix.trace.TracePlugin;
import com.tencent.matrix.trace.config.TraceConfig;
import com.tencent.matrix.util.MatrixLog;
import com.tencent.sqlitelint.SQLiteLint;
import com.tencent.sqlitelint.SQLiteLintPlugin;
import com.tencent.sqlitelint.config.SQLiteLintConfig;
import com.zxy.study.matrix.DynamicConfigImplDemo;
import com.zxy.study.matrix.TestPluginListener;

/**
 * @author : Shonn.Zhang
 * @email : shonn.zhang@mart.blue
 * @time : 2020/10/22 18:08
 * @desc :
 */
public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        BlockCanary.install(this, new AppBlockCanaryContext()).start();

        initMatrixTrace();
    }

    private void initMatrixTrace() {
        Matrix.Builder builder = new Matrix.Builder(this); // build matrix
        builder.patchListener(new TestPluginListener(this)); // add general pluginListener
        DynamicConfigImplDemo dynamicConfig = new DynamicConfigImplDemo(); // dynamic config

        //trace
        TraceConfig traceConfig = new TraceConfig.Builder()
                .dynamicConfig(dynamicConfig)
                .enableEvilMethodTrace(true)
                .enableAnrTrace(true)
                .enableStartup(true)
                .enableFPS(true)
                .isDebug(true)
                .isDevEnv(true)
                .build();
        TracePlugin tracePlugin = new TracePlugin(traceConfig);
//        builder.plugin(tracePlugin);

        // io
        IOCanaryPlugin ioCanaryPlugin = new IOCanaryPlugin(new IOConfig.Builder()
                .dynamicConfig(dynamicConfig)
                .build());
//        builder.plugin(ioCanaryPlugin);

        //resource
        Intent intent = new Intent();
        ResourceConfig.DumpMode mode = ResourceConfig.DumpMode.AUTO_DUMP;
        intent.setClassName(this.getPackageName(), "com.tencent.mm.ui.matrix.ManualDumpActivity");
        ResourceConfig resourceConfig = new ResourceConfig.Builder()
                .dynamicConfig(dynamicConfig)
                .setAutoDumpHprofMode(mode)
//                .setDetectDebuger(true) //matrix test code
                .setNotificationContentIntent(intent)
                .build();
        ResourcePlugin resourcePlugin = new ResourcePlugin(resourceConfig);
        builder.plugin(resourcePlugin);
        ResourcePlugin.activityLeakFixer(this);

        //sqlite
        SQLiteLintConfig sqlLiteConfig;
        try {
            sqlLiteConfig = new SQLiteLintConfig(SQLiteLint.SqlExecutionCallbackMode.CUSTOM_NOTIFY);
        } catch (Throwable t) {
            sqlLiteConfig = new SQLiteLintConfig(SQLiteLint.SqlExecutionCallbackMode.CUSTOM_NOTIFY);
        }
//        builder.plugin(new SQLiteLintPlugin(sqlLiteConfig));

        //init matrix
        Matrix.init(builder.build());

        // start plugin
//        tracePlugin.start();
        resourcePlugin.start();
    }
}
